package com.action.gulimall.order.listener;

import com.action.common.constant.MqConstant;
import com.action.common.constant.OrderConstant;
import com.action.common.to.mq.SeckillOrderTo;
import com.action.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author sl shilun217@qq.com
 * @Deacription 秒杀创单
 * @date 2021/07/05/16:35
 **/
@Slf4j
@RabbitListener(queues = {MqConstant.SECKILL_ORDER_TO_CREATE_QUEUE_NAME})
@Component
public class OrderSeckillListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void toCreateOrder(Message message, SeckillOrderTo seckillOrderTo,Channel channel) throws IOException {
        log.info("收到秒杀过来的秒杀信息，开始创建订单");
        try{
            orderService.createSeckillOrder(seckillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch(Exception e){
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
}
